//
//  OneDatBase.m
//  bh
//
//  Created by lanou on 15/12/26.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "OneDatBase.h"

@implementation OneDatBase


static OneDatBase *dataBase;

+ (OneDatBase *)shareSqliteManger{
    static dispatch_once_t onceToKen;
    dispatch_once(&onceToKen,^{
        
        dataBase = [[OneDatBase alloc]init];
    });
    return dataBase;
}

//指定数据的路径
- (NSString *)sqlitePaths:(NSString *)sqlitePaths{
    
    //先设置路径，然后拼接slitePaths（数据库的名字）
    NSString *sqlPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingString:sqlitePaths];
    
    NSLog(@"%@",sqlPath);

    //返回一个以路径 + 数据库名字的字符串给我们
    return sqlPath;
}

//打开数据库的方法
- (void)openSqliteWithPaths:(NSString *)paths{
    //获取数据库路径
    //paths 为我们数据库名字
    NSString *sqlitePath = [self sqlitePaths:paths];
    self.dataBase = [FMDatabase databaseWithPath:sqlitePath];
}


//关闭数据库
- (void)closeSqlite{
    [self.dataBase close];
}


//创建一个数据库表
- (void)createSqliteTableWithName:(NSString *)name{
    
    //判断——database是否存在（是否打开）
    if (!_dataBase) {
        //如果存在，我们调用创建打开的方法
        [self openSqliteWithPaths:[NSString stringWithFormat:@"/%@.db",name]];
    }
    if ([_dataBase open]) {
        BOOL SqliteName = [self.dataBase executeUpdate:@"CREATE TABLE ViewList(AuthorID Integer PRIMARY KEY,Col_Count TEXT ,Pic TEXT)"];//存入里面的一个是author_id col_count  pic,一共三个
        if (SqliteName) {
            NSLog(@"创建成功");
        }else{
            NSLog(@"建表失败或已经创建");
        }
    }
}


//插入一条数据
- (void)insertModel:(Picture *)model{
    if (!model) {
        NSLog(@"我们的model为空");
        return;
    }
    
    if ([_dataBase open]) {
        NSString *insertModel = [NSString stringWithFormat:@"insert into ViewList(AuthorID,Col_Count,Pic) values(?,?,?)"];
        BOOL insertSqlite = [_dataBase executeUpdate:insertModel,model.author_id,model.col_count,model.pic];
        if (insertSqlite) {
            
            NSLog(@"插入成功");
        }else{
            NSLog(@"插入失败");
        }
    }
}

//删除一条数据
- (void)deleteWWithAuthorID:(NSString *)authorID{
    
    if ([_dataBase open]) {
        NSString *deleteSqlite = [NSString stringWithFormat:@"delete from ViewList where AuthorID = ?"];
        
        BOOL deletePicture = [_dataBase executeUpdate:deleteSqlite,[authorID integerValue]];
        if (deletePicture) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
    }
}

//查找全部信息
- (NSArray *)selectAllModel{
    
    NSMutableArray *allModel = [[NSMutableArray alloc]init];
    
    if ([_dataBase open]) {
        
        FMResultSet *resultSet = [_dataBase executeQuery:@"select * from ViewList"];
        
        while ([resultSet next]) {
            
            Picture *picture = [[Picture alloc]init];
            picture.author_id = [resultSet stringForColumn:@"AuthorID"];
            picture.col_count = [resultSet stringForColumn:@"Col_Count"];
            picture.pic = [resultSet stringForColumn:@"Pic"];
            [allModel addObject:picture];
        }
    }
    [_dataBase close];
    return allModel;
}











@end
